#include <nxos.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

/**
 * FSTest client
 * 
 * 使用override宏去重定义客户端。如果有用户宏，那么就调用客户端。
 */

int main(int argc, char **argv)
{
    if (argc < 2)
    {
        printf("Usage: read <file>\n");
        return -1;
    }

    const char *op = argv[1];
    if (strcmp(op, "read") == 0)
    {
        if (argc < 3)
        {
            printf("Usage: read <file>\n");
            return -1;
        }
        const char *file = argv[2];
        int solt = NX_FileOpen(file, NX_FILE_RDWR, 0);
        if (solt < 0)
        {
            printf("Open file %s failed!\n", file);
            return -1;
        }
        char buf[1024];
        int len = NX_FileRead(solt, buf, 1024);
        if (len < 0)
        {
            printf("Read file %s failed!\n", file);
            return -1;
        }
        printf("Content: %s\n", buf);
        NX_FileClose(solt);
    }
    else if (strcmp(op, "write") == 0)
    {
        if (argc < 4)
        {
            printf("Usage: write <file> <content>\n");
            return -1;
        }
        const char *file = argv[2];
        int solt = NX_FileOpen(file, NX_FILE_RDWR | NX_FILE_CREAT, NX_FILE_MODE_WRITE | NX_FILE_MODE_READ);
        if (solt < 0)
        {
            printf("Open file %s failed!\n", file);
            return -1;
        }

        char buf[1024] = {0};
        int pos = 0;
        for (int i = 3; i < argc; ++i)
        {
            int len = strlen(argv[i]);
            memcpy(buf + pos, argv[i], len);
            pos += len;
            buf[pos++] = ' '; 
        }

        int len = NX_FileWrite(solt, buf, sizeof(buf));
        if (len < 0)
        {
            printf("Write file %s failed!\n", file);
            return -1;
        }
        
        NX_FileClose(solt);
    }
    else if (strcmp(op, "mkdir") == 0)
    {

        const char* dir = argv[2];
        int ret = NX_DirCreate(dir, 0777);
        if (ret < 0)
        {
            printf("Mkdir %s failed!\n", dir);
            return -1;
        }
        printf("Mkdir %s success!\n", dir);
    }
    else if (strcmp(op, "rmdir") == 0)
    {
        const char* dir = argv[2];
        int ret = NX_DirDelete(dir);
        if (ret < 0)
        {
            printf("Rmdir %s failed!\n", dir);
            return -1;
        }
    }
    else if (strcmp(op, "ls") == 0)
    {
        const char* dir = argv[2];
        int solt = NX_DirOpen(dir);
        if (solt < 0)
        {
            printf("Open dir %s failed!\n", dir);
            return -1;
        }
        NX_Dirent dirent;
        while (NX_DirRead(solt, &dirent) == NX_EOK)
        {
            printf("%s\n", dirent.name);
        }
        NX_DirClose(solt);
    }
    else if (strcmp(op, "rm") == 0)
    {
        const char* file = argv[2];
        int ret = NX_FileDelete(file);
        if (ret < 0)
        {
            printf("Unlink %s failed!\n", file);
            return -1;
        }  
    }
    else
    {
        printf("Unknown operation %s\n", op);
        return -1;
    }
    return 0;
}
